package com.magicborrow.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.utils.UIHelper;


public class IPublishedActivity extends TabActivity implements View.OnClickListener {

    private TabHost tabHost;
    private Intent homeIntent;
    private Intent findIntent;
    private ImageView mBack;
    private TextView alreadyPublished;
    private TextView draft;

    private LinearLayout llTop;
    private int mCurrentPosition=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ipublished_activity);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int statusBarHeight = UIHelper.getStatusHeight(this);
            ViewGroup vg = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            View v = new View(this);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
            v.setLayoutParams(params);
            v.setBackgroundColor(getResources().getColor(R.color.title_color));
            vg.addView(v, 0);

        }

        initView();
        initListener();
        tabHost = getTabHost();
        initIntent();
        initTabs();
    }

    private void initView(){
        mBack = (ImageView)findViewById(R.id.back);
        alreadyPublished = (TextView)findViewById(R.id.alreadyPublished);
        draft = (TextView)findViewById(R.id.draft);
        llTop = (LinearLayout) findViewById(R.id.ll_top);
        change();
    }

    private void change() {
        for (int i = 0; i < llTop.getChildCount(); i++) {
            if (mCurrentPosition==i){
                TextView childAt1 = (TextView) llTop.getChildAt(mCurrentPosition);
                childAt1.setBackgroundResource(mCurrentPosition==0?R.drawable.round_inter_left:
                        R.drawable.round_inter_right);
                childAt1.setTextColor(getResources().getColor(R.color.black));
            }else{
                TextView childAt2 = (TextView) llTop.getChildAt(i);
                childAt2.setBackgroundResource(mCurrentPosition==0?R.drawable.round_inter_yright:
                        R.drawable.round_inter_yleft);
                childAt2.setTextColor(getResources().getColor(R.color.ipulish_left_textclor));
            }
        }
    }

    private void initListener(){
        mBack.setOnClickListener(this);
        alreadyPublished.setOnClickListener(this);
        draft.setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(this);
    }

    private void initTabs() {
        tabHost.addTab(buildTbs("find", findIntent));
        tabHost.addTab(buildTbs("home", homeIntent));
    }

    private TabHost.TabSpec buildTbs(String tag, Intent intent) {
        return tabHost.newTabSpec(tag)
                .setIndicator(tag)
                .setContent(intent);
    }

    private void initIntent() {
        findIntent = new Intent(this, PublishedActivity.class);
        homeIntent = new Intent(this, DraftActivity.class);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.alreadyPublished:
                tabHost.setCurrentTab(0);
                mCurrentPosition=0;
                change();
                break;
            case R.id.draft:
                tabHost.setCurrentTab(1);
                mCurrentPosition=1;
                change();
                break;
            case R.id.back:
                finish();
                break;
        }
    }
}
